package com.aua.book.android.xml;

import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;

public class MyGestureDetector extends SimpleOnGestureListener {

  AndroidBookActivity app;

  public MyGestureDetector(AndroidBookActivity app) {
    super();
    this.app = app;
  }

  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    int currentPage = app.getCurrnetPage();
    if (e1.getX() - e2.getX() > Integer.parseInt(app.getString(R.string.swipeMinDistance))
        && Math.abs(velocityX) > Integer.parseInt(app.getString(R.string.swipeThresholdVelocity))) {

      // Toast.makeText(app, "next", Toast.LENGTH_SHORT).show();

      if (currentPage < app.getLastPage() - 1) {
        currentPage = PageTurnHelper.turnRight(app,app.getProgressTextView(), app.getTextView(), app.getPages(),
            app.getCurrnetPage(), app.getLastPage());
        app.setCurrnetPage(currentPage);
      }
    } else if (e2.getX() - e1.getX() > Integer.parseInt(app.getString(R.string.swipeMinDistance))
        && Math.abs(velocityX) > Integer.parseInt(app.getString(R.string.swipeThresholdVelocity))) {

      if (currentPage > 0) {
        currentPage = PageTurnHelper.turnLeft(app,app.getProgressTextView(), app.getTextView(), app.getPages(),
            app.getCurrnetPage(), app.getLastPage());
        app.setCurrnetPage(currentPage);
        // Toast.makeText(app, "previous", Toast.LENGTH_SHORT).show();
      }
    }

    return false;
  }
}
